package com.h.heal.controller;

import com.h.heal.common.MyPageInfo;
import com.h.heal.entity.Topic;
import com.h.heal.mapper.TopicMapper;
import com.h.heal.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.util.List;

@Controller
@RequestMapping("/topic")
public class TopicController {
    @Autowired
    private TopicService topicService;
    @Autowired
    private TopicMapper topicMapper;

    @RequestMapping("/main")
    public ModelAndView main(HttpServletRequest request, ModelAndView mv) {
        mv.setViewName("/admin/topic/main");
        MyPageInfo myPageInfo = topicService.findBySearch(request);
        mv.addObject("pageInfo", myPageInfo);
        return mv;
    }

    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request, ModelAndView mv) {
        mv.setViewName("/user/topic/index");
        List<Topic> topic = topicService.findAll();
        mv.addObject("list", topic);
        return mv;
    }


    @RequestMapping("/user/main")
    public ModelAndView userMain(HttpServletRequest request, ModelAndView mv) {
        mv.setViewName("/user/topic/main");
        MyPageInfo myPageInfo = topicService.findBySearchUser(request);
        mv.addObject("p+ageInfo", myPageInfo);
        return mv;
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        topicService.deleteById(id);
        return "redirect:/topic/main";
    }

    @RequestMapping("/user/delete/{id}")
    public String userDelete(@PathVariable("id") int id) {
        topicService.deleteById(id);
        return "redirect:/topic/user/main";
    }

    @RequestMapping("/create")
    @ResponseBody
    public String create(Topic topic) {

        return topicService.create(topic);
    }

    @RequestMapping("/openEdit/{id}")
    public ModelAndView openEdit(@PathVariable("id") int id, ModelAndView mv) {
        Topic topic = topicService.findById(id);
        mv.setViewName("/admin/topic/edit");
        System.out.println(topic);
        mv.addObject("topic", topic);
        return mv;
    }

    @RequestMapping("/doEdit")
    @ResponseBody
    public String doEdit(Topic topic) {
        return topicService.edit(topic);
    }

    @RequestMapping("/user/openEdit/{id}")
    public ModelAndView userOpenEdit(@PathVariable("id") int id, ModelAndView mv) {
        Topic topic = topicService.findById(id);
        mv.setViewName("/user/topic/edit");
        System.out.println(topic);
        mv.addObject("topic", topic);
        return mv;
    }

    @RequestMapping("/user/doEdit")
    @ResponseBody
    public String userDoEdit(Topic topic) {
        return topicService.edit(topic);
    }


//    @RequestMapping("/like")
//    public void like(Topic topic) {
//        topicMapper.like(topic);
//    }

    @RequestMapping("/like")
    @ResponseBody
    public String like(String id) {
        topicMapper.like(id);
        return "ok";  //重定向到登录页面

    }


    @RequestMapping("/user/browse/{id}")
    public ModelAndView userBrowse(@PathVariable("id") int id, ModelAndView mv) {
        Topic topic = topicService.findById(id);
        mv.setViewName("/user/topic/browse");
        System.out.println(topic);
        mv.addObject("topic", topic);
        return mv;
    }
}




